package com.example.administrator.myapplication.download;

import android.database.DatabaseUtils;
import android.databinding.DataBindingUtil;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;

import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.databinding.ActivityHotCommentBinding;

import java.util.ArrayList;
import java.util.List;

public class HotCommentActivity extends AppCompatActivity {
  private ActivityHotCommentBinding binding;
    private FragmentTransaction transaction;
    private     List<Fragment> fragments;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_hot_comment);
        aboutFragmentTransaction();
        aboutRadioGroup();
    }

    private void aboutFragmentTransaction() {
        transaction = getSupportFragmentManager().beginTransaction();
         fragments = new ArrayList<>();
        fragments.add(HotCommentCommFragment.newInstance("最火",1));
        fragments.add(HotCommentCommFragment.newInstance("最新",2));
        fragments.add(HotCommentCommFragment.newInstance("最热",3));
        transaction.add(R.id.hot_comment_fragment,fragments.get(0)).show(fragments.get(0)).hide(fragments.get(1)).hide(fragments.get(2));
        transaction.add(R.id.hot_comment_fragment,fragments.get(1));
        transaction.add(R.id.hot_comment_fragment,fragments.get(2));
        transaction.commit();
    }

    private void aboutRadioGroup() {
        binding.hotCommentGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                switch (checkedId) {
                    case R.id.hot_comment_but1:
                        binding.hotCommentBut1.setChecked(true);
                        binding.hotCommentBut1.setTextColor(getResources().getColor(R.color.colorWhite));
                        binding.hotCommentBut2.setTextColor(getResources().getColor(R.color.colorRed));
                        binding.hotCommentBut3.setTextColor(getResources().getColor(R.color.colorRed));
                        transaction.show(fragments.get(0)).hide(fragments.get(1)).hide(fragments.get(2));
                        break;
                    case R.id.hot_comment_but2:
                        binding.hotCommentBut2.setChecked(true);
                        binding.hotCommentBut2.setTextColor(getResources().getColor(R.color.colorWhite));
                        binding.hotCommentBut1.setTextColor(getResources().getColor(R.color.colorRed));
                        binding.hotCommentBut3.setTextColor(getResources().getColor(R.color.colorRed));
                        transaction.show(fragments.get(1)).hide(fragments.get(0)).hide(fragments.get(2));
                        break;
                    case R.id.hot_comment_but3:
                        binding.hotCommentBut3.setChecked(true);
                        binding.hotCommentBut3.setTextColor(getResources().getColor(R.color.colorWhite));
                        binding.hotCommentBut1.setTextColor(getResources().getColor(R.color.colorRed));
                        binding.hotCommentBut2.setTextColor(getResources().getColor(R.color.colorRed));
                        transaction.show(fragments.get(2)).hide(fragments.get(1)).hide(fragments.get(0));
                        break;
                }
                transaction.commit();
            }
        });
    }
}
